package com.lihao.wanandroid.ui.project

import android.app.Application
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import com.lihao.jetpackcore.base.BaseViewModel
import com.lihao.jetpackcore.ext.request
import com.lihao.jetpackcore.state.ResultState
import com.lihao.wanandroid.data.AppRepository
import com.lihao.wanandroid.data.model.ArticleResponse
import com.lihao.wanandroid.data.model.BasePageResponse
import com.lihao.wanandroid.data.model.ProjectTreeResponse

/**
 * Created by lihao on 2020/5/19.
 * Describe:
 */
class ProjectViewModel(application: Application) : BaseViewModel(application) {

    val fragments: ArrayList<Fragment> = arrayListOf()

    val tabTitles: ArrayList<String> = arrayListOf()

    val projectTree: MutableLiveData<ResultState<ArrayList<ProjectTreeResponse>>> =
        MutableLiveData()

    val projectList: MutableLiveData<ResultState<BasePageResponse<ArticleResponse>>> =
        MutableLiveData()

    var isNewProject: Boolean = false

    var curViewPagerIndex = 0

    /**
     * 新项目的page从0开始，分类下的项目列表从1开始
     */
    var page = 0

    var id = 0

    /**
     * 获取项目分类
     */
    fun getProjectTree() {
        request({ AppRepository.getProjectTree() }, projectTree)
    }

    /**
     * 获取项目分类下的列表
     */
    fun getProjectList() {
        request({ AppRepository.getProjectList(page, id) }, projectList)
    }

    /**
     * 获取新项目列表
     */
    fun getNewProjectList() {
        request(
            { AppRepository.getNewProjectList(page) },
            projectList
        )
    }

    fun autoGetProjectList(isRefresh: Boolean) {
        if (isNewProject) {
            if (isRefresh) page = 0
            getNewProjectList()
        } else {
            if (isRefresh) page = 1
            getProjectList()
        }
    }

}